/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sqlserver;

import java.awt.Component;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;

/**
 *
 * @author PHAMDANH
 */
public class ColumnNameCellRenderer extends DefaultTableCellRenderer 
{
    private ModifyPanel panel;
    public ColumnNameCellRenderer(ModifyPanel panel) 
    {
        this.panel = panel;
    }
    @Override
    public Component getTableCellRendererComponent(JTable jtable, Object value, 
                                                    boolean isSelected, boolean hasFocus, int row, int column) 
    {            
        super.getTableCellRendererComponent(jtable, value, isSelected, hasFocus, row, column);                    
        if(value!=null)
        {
            String name = value.toString();
            boolean isExist = false;
            for(int i=0;i<panel.getTable().getColumns().size();i++)
            {
                if(i!=row&&panel.getTable().getColumns().get(i).getName().equals(name))
                {
                    isExist = true;
                    break;
                }
            }
            if(row<panel.getTable().getColumns().size())
            {
                String lastName = panel.getTable().getColumns().get(row).getName();
                if(!isExist)
                {            
                    if(!"".equals(name)&&IsValidName(name))
                        this.panel.getTable().getColumns().get(row).setName(name);
                    else
                    {                            
                        jtable.getModel().setValueAt(lastName, row, column);                                                    
                        panel.InitTableModel(); 
                    }
                }            
                else
                {                                    
                    jtable.getModel().setValueAt(lastName, row, column);
                    JOptionPane.showMessageDialog(panel, "Trùng column");                                                 
                    panel.InitTableModel(); 
                }
            }
            else
            {
                if(isExist)
                {
                    jtable.getModel().setValueAt("", row, column);
                    JOptionPane.showMessageDialog(panel, "Trùng column");                        
                    panel.InitTableModel(); 
                }
                else                        
                    if(!"".equals(name)&&IsValidName(name))
                    {
                        Object objType = jtable.getModel().getValueAt(row, column+1);
                        String strType;
                        if(objType!=null)
                            strType=objType.toString();
                        else
                            strType="String";
                        Class type=Integer.class;
                        if("String".equals(strType))
                            type = String.class;
                        Object objAllowNull=jtable.getModel().getValueAt(row, column+2);
                        Boolean allowNull;
                        if(objAllowNull!=null)
                            allowNull=(Boolean)objAllowNull;
                        else
                            allowNull=false;
                        if(!panel.getTable().AddColumn(name, type, allowNull))
                            JOptionPane.showMessageDialog(null, "Không thể thêm cột");
                        panel.InitTableModel();  
                    }
                    else
                    {
                        jtable.getModel().setValueAt("", row, column);                                                    
                    }
                          
            }
        }
        else
            jtable.getModel().setValueAt("", row, column);
        return this;
    }
    public boolean IsValidName(String name)
    {
        for(int i=0;i<name.length();i++)
        {
            char ch= name.charAt(i);
            if((ch>=97&&ch<=122)||(ch>=65&&ch<=90)||(ch>=48&&ch<=57)||ch==95)
                continue;
            else
                return false;
        }
        return true;
    }
}
    
